package com.tan.controller;

import com.tan.domain.User;
import com.tan.service.UserService;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author by 团子
 * @Classname UserController
 * @Description TODO
 * @Date 2022/1/7 10:09
 * @Created by 团子
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RestHighLevelClient client;

    @RequestMapping("/findAll")
    public List<User> findAll() throws Exception {
        return userService.findAll();
    }

    @RequestMapping("/save")
    public String save(){
        User user = new User();
        user.setUname("张三");
        user.setAge(18);
        user.setSex(1);
        return userService.save(user);
    }

    /**
     * 添加索引
     */
    @RequestMapping("/addIndex")
    public void addIndex() throws Exception {
        //1.使用client获取操作索引的对象
        IndicesClient indicesClient = client.indices();
        //2.具体操作，获取返回值
        CreateIndexRequest createRequest = new CreateIndexRequest("abc");
        CreateIndexResponse response = indicesClient.create(createRequest, RequestOptions.DEFAULT);

        //3.根据返回值判断结果
        System.out.println(response.isAcknowledged());
    }
}
